Skip to content

Latest commit

 

History

History
77 lines (62 loc) · 2.76 KB

File metadata and controls

77 lines (62 loc) · 2.76 KB

2301. Match Substring After Replacement

You are given two strings s and sub. You are also given a 2D character array mappings where mappings[i] = [oldi, newi] indicates that you may perform the following operation any number of times:

  • Replace a character oldi of sub with newi.

Each character in subcannot be replaced more than once.

Return trueif it is possible to makesuba substring ofsby replacing zero or more characters according tomappings. Otherwise, return false.

A substring is a contiguous non-empty sequence of characters within a

Example 1:

Input: s = "fool3e7bar", sub = "leet", mappings = [["e","3"],["t","7"],["t","8"]] Output: true Explanation: Replace the first 'e' in sub with '3' and 't' in sub with '7'. Now sub = "l3e7" is a substring of s, so we return true. 

Example 2:

Input: s = "fooleetbar", sub = "f00l", mappings = [["o","0"]] Output: false Explanation: The string "f00l" is not a substring of s and no replacements can be made. Note that we cannot replace '0' with 'o'. 

Example 3:

Input: s = "Fool33tbaR", sub = "leetd", mappings = [["e","3"],["t","7"],["t","8"],["d","b"],["p","b"]] Output: true Explanation: Replace the first and second 'e' in sub with '3' and 'd' in sub with 'b'. Now sub = "l33tb" is a substring of s, so we return true. 

Constraints:

  • 1 <= sub.length <= s.length <= 5000
  • 0 <= mappings.length <= 1000
  • mappings[i].length == 2
  • oldi != newi
  • s and sub consist of uppercase and lowercase English letters and digits.
  • oldi and newi are either uppercase or lowercase English letters or digits.

Solutions (Rust)

1. Solution

implSolution{pubfnmatch_replacement(s:String,sub:String,mappings:Vec<Vec<char>>) -> bool{let s = s.as_bytes();let sub = sub.as_bytes();letmut bitmappings = [0_i128;128];for i in0..mappings.len(){let(old, new) = (mappings[i][0]asusize, mappings[i][1]asusize); bitmappings[old] |= 1 << new;}for i in0..=s.len() - sub.len(){letmut flag = true;for j in0..sub.len(){if sub[j] != s[i + j] && bitmappings[sub[j]asusize]&(1 << s[i + j]) == 0{ flag = false;break;}}if flag {returntrue;}}false}}
close